<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="params.entity" style="padding-bottom: 0px;">
          <el-form-item label="同步时间">
<!--            <el-input v-model="params.entity.startTime" placeholder="请输入开始时间"/>-->
            <el-date-picker
                v-model="searchDate"
                type="datetimerange"
                :size="size"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="searchData">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table v-loading="loading"
              :data="tableData.list" style="width: 100%" height="500"
              :default-sort="{ prop: 'date', order: 'descending' }" @selection-change="handleSelectionChange">
      <el-table-column prop="cronTriggersClass" label="计划类名" show-overflow-tooltip/>
      <el-table-column prop="syncTime" label="同步时间" show-overflow-tooltip/>
      <el-table-column prop="startTime" label="开始时间" show-overflow-tooltip/>
      <el-table-column prop="entTime" label="结束时间" show-overflow-tooltip/>
      <el-table-column prop="execTimes" label="执行时长(秒)" show-overflow-tooltip/>
<!--      <el-table-column prop="createUser" label="创建人" show-overflow-tooltip/>-->
<!--      <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip/>-->
<!--      <el-table-column prop="updateTime" label="创建时间" show-overflow-tooltip/>-->
      <el-table-column prop="remark" label="备注" show-overflow-tooltip/>
    </el-table>

    <el-pagination v-model:currentPage="params.current" v-model:page-size="params.size"
                   :page-sizes="[10, 20, 50, 100]" layout="sizes, prev, pager, next" :total="tableData.total"
                   @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </el-card>

  <!-- 新增或者修改页面-->
  <CronTriggersSynchroTimeAddOrEdit
      @closeEditDialog="closeEditDialog"
      :addOrEditVisible="addOrEditVisible"
      :ruleForm="ruleForm"
      :addDataType="addDataType"></CronTriggersSynchroTimeAddOrEdit>
</template>

<script lang="ts" setup>
import {ElTable, ElMessage, ElMessageBox} from 'element-plus'
import {onMounted, computed, reactive, ref, watch} from 'vue'
import {Params, Result} from "@/apis/model/common/common.interfaces"
import {Delete, Edit, Search, Share, Upload, Plus, Setting} from '@element-plus/icons-vue'
import {CronTriggersSynchroTime} from "@/apis/model/system/CronTriggersSynchroTimeModel";
import {
  cronTriggersSynchroTimePage,
  deleteCronTriggersSynchroTimeById
} from "@/apis/http/system/CronTriggersSynchroTimeHttp";
import {CronTriggers} from "@/apis/model/system/CronTriggersModel";

// 查询
const params: Params = reactive({
  current: 1,
  size: 10,
  search: '',
  entity: {}
})

const searchDate = ref([])

interface Prop {
  ruleForm: CronTriggers
}

// 传递过来的参数
const props = defineProps<Prop>();

const ruleForm = ref<CronTriggersSynchroTime>({} as CronTriggersSynchroTime)

// AddOrEditVisible
const addOrEditVisible = ref(false)

// table 表数据
const tableData: any = ref({});

// 加载数据
const loading = ref(true)
const addDataType = ref('add');

/**
 * 初始化数据
 */
const initList = () => {
  loading.value = true
  cronTriggersSynchroTimePage(params).then((res: Result) => {
    const code = res.code
    const message = res.message
    const data = res.data
    if (code == 200) {
      tableData.value = data
    }
    loading.value = false
  })
}

/**
 * search 查询数据
 */
const searchData = () => {
  params.entity.startSyncTime = searchDate.value[0];
  params.entity.endSyncTime = searchDate.value[1];
  initList()
}

/**
 * 每页数据量改变查询数据
 * @param val
 */
const handleSizeChange = (val: number) => {
  params.size = val
  initList()
}

/**
 * 分页查询数据
 * @param val
 */
const handleCurrentChange = (val: number) => {
  params.current = val
  initList()
}

/**
 * 添加弹出
 */
const addDialog = () => {
  addOrEditVisible.value = true
  ruleForm.value = {} as CronTriggersSynchroTime
  addDataType.value = 'add'
}

//修改弹窗
const editDialog = (row: CronTriggersSynchroTime) => {
  addOrEditVisible.value = true
  ruleForm.value = row
  addDataType.value = 'edit'
}

// 关闭修改弹窗
const closeEditDialog = (e: any) => {
  addOrEditVisible.value = e.addOrEditVisible
  if (e.refushList = true) {
    params.current = 1;
    initList();
  }
}

/**
 * 弹出删除对话框
 */
const showDeleteDialog = (id: number) => {
  ElMessageBox.confirm(
      '是否删除?',
      '提醒',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    deleteCronTriggersSynchroTimeById(id).then((res: Result) => {
      const code = res.code;
      if (code == 200) {
        ElMessage({
          message: '删除数据成功！',
          type: 'success',
        })
        initList();
      } else {
        ElMessage.error('删除数据失败！');
      }
    })
  }).catch(() => {
    // ElMessage({
    //   type: 'info',
    //   message: 'Delete canceled',
    // })
  })
}

watch(() => props.ruleForm, () =>{
  params.entity.triggersId = props.ruleForm.id
  initList()
})

onMounted(() => {
  params.entity.triggersId = props.ruleForm.id
  initList()
})
</script>

<style>
</style>
